/*
 * @Author: C.
 * @Date: 2023-04-06 14:35:39
 */
import { handleRouteName, handleEjsToFile } from '../utils';
const tem_config = 'config_ts.ejs';
const tem_page = 'page_ts.ejs';
export const baseWorkDir = 'src/views/work/';
/**
 * @description: 根据菜单树 生成对应的文件
 * @param {*} menuList
 * @param {*} baseDir
 * @return {*}
 */
export function genMenuFile(menuList, baseDir = baseWorkDir) {
  menuList.forEach((item) => {
    // handleEjsToFile(item.name, `src/views/work/${lowerName(item.name)}`, temSrc, `index.vue`);
    if (item?.children) {
      genMenuFile(item?.children, `${baseDir}${handleRouteName(item.name)}`);
    } else {
      if (item.isCURD) {
        genFiles(item, baseDir, 'main');
        genFiles(item, baseDir, 'detail');
        genFiles(item, baseDir, 'edit');
      } else {
        genFiles(item, baseDir, 'main');
      }
    }
  });
}

export function genFiles(menuItem, baseDir, type = 'main') {
  if (type == 'main') {
    handleEjsToFile({
      name: menuItem.name,
      dest: `${baseDir}/${handleRouteName(menuItem.name)}`,
      template: `./template/${type}/${tem_config}`,
      filename: `schema.data.ts`,
      title: menuItem.title,
      apiModule: getApiPath(baseDir),
    });
    handleEjsToFile({
      name: menuItem.name,
      dest: `${baseDir}/${handleRouteName(menuItem.name)}`,
      template: `./template/${type}/${tem_page}`,
      filename: `index.vue`,
      title: menuItem.title,
      apiModule: getApiPath(baseDir),
    });
  } else {
    handleEjsToFile({
      name: menuItem.name,
      dest: `${baseDir}/${handleRouteName(menuItem.name)}/${type}`,
      template: `./template/${type}/${tem_config}`,
      filename: `schema.data.ts`,
      title: menuItem.title,
      apiModule: getApiPath(baseDir),
    });
    handleEjsToFile({
      name: menuItem.name,
      dest: `${baseDir}/${handleRouteName(menuItem.name)}/${type}`,
      template: `./template/${type}/${tem_page}`,
      filename: `index.vue`,
      title: menuItem.title,
      apiModule: getApiPath(baseDir),
    });
  }
}
function getApiPath(path: string) {
  return path.replace(baseWorkDir, '');
}
